﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using ScrumTable.Common;
using ScrumTable.Common.Security;

#endregion

namespace ScrumTable.DL.Data.TFS.Services.TeamFoundation
{
    /// <summary>
    /// Wraps the internal TeamFoundationServer instance.
    /// </summary>
    public partial class TfsServerWrapper : DisposeBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private TfsTeamProjectCollection _teamFoundationServer;

        private readonly WorkItemStore _workItems;
        private readonly IRegistration _registrations;
        private readonly IIdentityManagementService _identityManagement;
        private readonly ConnectionInformation _connectionInfo;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Retruns true if the current user has been autheticated on the
        /// server side.
        /// </summary>
        public bool HasAuthenticated
        {
            get { return _teamFoundationServer.HasAuthenticated; }
        }

        /// <summary>
        /// Gets the TFS work item store.
        /// </summary>
        public WorkItemStore WorkItems
        {
            get { return _workItems; }
        }

        /// <summary>
        /// Gets the TFS registration store.
        /// </summary>
        public IRegistration Registrations
        {
            get { return _registrations; }
        }

        /// <summary>
        /// Gets the TFS identity management.
        /// </summary>
        public IIdentityManagementService IdentityManagement
        {
            get { return _identityManagement; }
        }

        /// <summary>
        /// Gets the authorized team foundation identity information.
        /// </summary>
        public TeamFoundationIdentity AuthorizedIdentity
        {
            get { return _teamFoundationServer.ConfigurationServer.AuthorizedIdentity; }
        }

        /// <summary>
        /// Gets the internal connection information.
        /// </summary>
        public ConnectionInformation ConnectionInformation
        {
            get { return _connectionInfo; }
        }

        /// <summary>
        /// Gets the native team foundation server connection which is used to
        /// connect to the team foundation services. 
        /// </summary>
        public TfsConnection NativeConnection { get { return _teamFoundationServer; } }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new TfsServerWrapper instance.
        /// </summary>
        public TfsServerWrapper(ConnectionInformation connectionInfo, Uri endSystemUri)
        {
            PreCondition.AssertNotNull(connectionInfo, "connectionInfo");

            _teamFoundationServer = new TfsTeamProjectCollection(
                endSystemUri,
                connectionInfo.NetworkCredentials);

            _connectionInfo = connectionInfo;
            _workItems = GetService<WorkItemStore>();
            _registrations = GetService<IRegistration>();
            _identityManagement = GetService<IIdentityManagementService>();

            Initialize();
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Ensures a connection to the team foundation server has been established
        /// and can be used in order to talk to its services.
        /// </summary>
        public bool EnsureConnected()
        {
            EnsureAuthenticated();
            return HasAuthenticated;
        }

        /// <summary>
        /// Ensures that the current user is authenticated.
        /// </summary>
        public void EnsureAuthenticated()
        {
            _teamFoundationServer.EnsureAuthenticated();
        }

        /// <summary>
        /// Gets the service with the associated type.
        /// </summary>
        /// <typeparam name="T">Type of the service to retreive.</typeparam>
        /// <returns>Returns the retreived service.</returns>
        public T GetService<T>()
        {
            return (T)_teamFoundationServer.GetService(typeof(T));
        }

        /// <summary>
        /// Releases managed (c# or .net) resources.
        /// </summary>
        /// <remarks>
        /// Disposes the team foundation server connection and releases
        /// the underlying resources.
        /// </remarks>
        protected override void ReleaseManagedResources()
        {
            _teamFoundationServer.Dispose();
            _teamFoundationServer = null;
        }

        /// <summary>
        /// Virual constructor call from partial class.
        /// </summary>
        partial void Initialize();

        #endregion

        #region Events

        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}